﻿// -----------------------------------------------------------------------
// <copyright file="Position.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using AdCom.Utility;
    using VDC.COMMON;

    /// <summary>
    /// Class Positions
    /// </summary>
    public class Position
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Position" /> class
        /// </summary>
        public Position()
        {
            this.PositionsId = 0;
            this.Code = string.Empty;
            this.Name = string.Empty;
        }
        
        /// <summary>
        /// Gets or sets PositionsId
        /// </summary>
        public int PositionsId { get; set; }

        /// <summary>
        /// Gets or sets Code
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// Gets or sets Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets Users
        /// </summary>
        public IList<UserDepartment> UserDepartments
        {
            get
            {
                try
                {
                    IList<UserDepartment> lstUserDepartment = new List<UserDepartment>();
                    var reader = SqlDataHelper.ExecuteReader(string.Format(Constants.StorepProcedure.VOneUserDepartmentGetByPositionId, this.PositionsId));
                    while (reader.Read())
                    {
                        UserDepartment userDepartment = new UserDepartment();

                        if (!reader.IsDBNull(0))
                        {
                            userDepartment.DepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            userDepartment.UserId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            userDepartment.DepartmentId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            userDepartment.PositionsId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            userDepartment.Active = reader.GetBoolean(4);
                        }

                        lstUserDepartment.Add(userDepartment);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstUserDepartment;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}